package it.eng.area118.mdogpscollector.command;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Set;

import org.garret.perst.Index;
import org.garret.perst.Storage;

import android.content.res.AssetFileDescriptor;
import android.content.res.AssetManager;
import android.os.Environment;
import android.support.v4.app.FragmentActivity;
import android.util.Log;
import it.eng.area118.mdocommon.android.command.AndroidCommand;
import it.eng.area118.mdocommon.android.util.AndroidApplicationContext;
import it.eng.area118.mdocommon.android.util.StringUtils;
import it.eng.area118.mdogpscollector.R;
import it.eng.area118.mdogpscollector.db.DbManager;
import it.eng.area118.mdogpscollector.fragment.CheckPointListFragment;
import it.eng.area118.mdogpscollector.model.CheckPoint;
import it.eng.area118.mdogpscollector.model.PerstRootBean;
import it.eng.area118.mdogpscollector.service.CheckPointLookupService;
import it.eng.area118.mdogpscollector.service.IndexLookupService;
import it.eng.area118.mdogpscollector.service.LookupService;

public class SearchCommand extends AndroidCommand<SearchFilter, List<CheckPoint>> {
	private CheckPointListFragment fragment;

	public SearchCommand(CheckPointListFragment fragment) {
		this.fragment = fragment;
		setTimeout(30);
	}

	@Override
	public void before() {

		fragment.setWaiting(true);
	}

	@Override
	public void after() {
		fragment.setWaiting(false);

	}

	@SuppressWarnings("unchecked")
	@Override
	protected List<CheckPoint> doCommand(SearchFilter... params) throws Exception {
		fragment.setWaiting(true);
		SearchFilter filter = params[0];
		if (filter != null) {
			Log.i("", "searchkeyyyyy: " + filter.getSearchKey());
			if (!filter.isOpenKmRange()) {
				return CheckPointLookupService.getService().lookup(filter.getSearchKey(), new KmComparator());
			} else {
				Log.i("filetr name", "name: " + filter.getName());
				Log.i("filetr km", "km: " + filter.getKm());
				List<CheckPoint> list = null;
				if (!StringUtils.isEmpty(filter.getName())) {
					Log.i("searching", "by name" + filter.getName());
					list = CheckPointLookupService.getService().lookup(filter.getName() + "-" + filter.getDirection(), new KmComparator());
				} else {
					Log.i("searching", "all" + filter.getName());
					list = CheckPointLookupService.getService().getAll(new KmComparator());
				}
				Log.i("searching", "filtering by km open range" + filter.getKm());
				int filtKm = Integer.valueOf(filter.getKm());
				List<CheckPoint> toReturn = new ArrayList<CheckPoint>();
				for (CheckPoint checkPoint : list) {
					int c = Integer.valueOf(checkPoint.getPoint());
					if (c >= filtKm) {
						toReturn.add(checkPoint);
					}
				}
				return toReturn;
			}
		} else {
			return CheckPointLookupService.getService().getAll(new KmComparator());
		}
	}

	private class KmComparator implements Comparator<CheckPoint> {

		@Override
		public int compare(CheckPoint lhs, CheckPoint rhs) {
			try {
				Integer p1 = Integer.valueOf(lhs.getPoint());
				Integer p2 = Integer.valueOf(rhs.getPoint());
				return p1.compareTo(p2);
			} catch (Exception e) {
				return 0;
			}
		}

	}
}
